package xiangMu.pc;

import java.util.Scanner;

public class PcTest {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        Pc pc = new Pc();
        System.out.print("新电脑采购成功，请输入型号：");
        pc.setSeries(input.next());
        System.out.print("请输入价格：");
        pc.setPrice(input.nextInt());
        System.out.print("请输入分辨率：");
        pc.setResolutionRatio(input.next());
        System.out.print("请输入硬盘容量(兆作为单位)：");
        pc.setDiskCapacity(input.nextInt());
        int choice;

        while (true) {
            System.out.println("1:格式化，2:安装操作系统，3:启动，4:安装软件，5:下载软件，6:查看各盘情况，9:退出");
            System.out.print("请选择：");
            choice = input.nextInt();
            if (choice == 1) {
                System.out.println("--格式化--");
                if (!pc.isState()) {
                    System.out.println("请先启动");
                    continue;
                }
                System.out.print("计划分成几个盘：");
                int number = input.nextInt();
                Disk[] disks = new Disk[number];
                for (int i = 0; i < number; i++) {
                    disks[i] = new Disk();
                }
                System.out.println("请输入" + number + "个盘符");
                for (int i = 0; i < number; i++) {
                    disks[i].setName(input.next());
                }
                System.out.println("请输入" + number + "个盘符的大小");
                for (int i = 0; i < number; i++) {
                    disks[i].setSize(input.nextInt());
                }
                if (pc.setDisks(disks))
                    System.out.println("格式化成功");
                else
                    System.out.println("格式化失败");
            } else if (choice == 2) {
                System.out.println("--安装操作系统--");
                if (!pc.isState()) {
                    System.out.println("请先启动");
                    continue;
                }
                if (!pc.judgeHasFormat()) {
                    System.out.println("请先格式化");
                    continue;
                }
                System.out.print("请选择操作系统：");
                for (int i = 0; i < os.length; i++) {
                    if (i < os.length - 1) {
                        System.out.print(os[i].getName() + "/");
                    } else
                        System.out.println(os[i].getName());
                }
                String osName;
                while (searchOS(osName = input.next()) == null)
                    System.out.print("没有这个操作系统，请重新输入：");
                System.out.print("请选择安装的盘：");
                for (int i = 0; i < pc.getDisks().length; i++) {
                    if (i < pc.getDisks().length - 1) {
                        System.out.print(pc.getDisks()[i].getName() + "/");
                    } else
                        System.out.println(pc.getDisks()[i].getName());
                }
                String diskName;
                while (pc.searchDisk(diskName = input.next()) == null)
                    System.out.print("没有这个盘，请重新输入：");
                if (pc.installOS(searchOS(osName), pc.searchDisk(diskName)))
                    System.out.println("操作系统安装成功");
                else
                    System.out.println("操作系统安装失败");

            } else if (choice == 3) {
                System.out.println("--启动--");
                pc.start();
            } else if (choice == 4) {
                System.out.println("--安装软件--");
                if (!judge(pc))
                    continue;
                System.out.println("1：普通软件/2：下载软件");
                int n;
                while ((n = input.nextInt()) != 1 && n != 2)
                    System.out.print("选择错误，请重新输入：");
                if (!(pc.showSoftware1(n)))
                    continue;
                System.out.print("软件名称：");
                String softwareName;
                while (pc.searchSoftware(softwareName = input.next()) == null)
                    System.out.print("没有此已下载软件，请重新输入：");
                System.out.print("安装位置：");
                String diskName;
                while (pc.searchDisk(diskName = input.next()) == null)
                    System.out.print("没有这个盘，请重新输入：");
                if (pc.installSoftware(pc.searchSoftware(softwareName), pc.searchDisk(diskName)))
                    System.out.println(pc.searchSoftware(softwareName).getName() + "安装成功");
                else
                    System.out.println(pc.searchSoftware(softwareName).getName() + "安装失败");
            } else if (choice == 5) {
                System.out.println("--下载软件--");
                if (!judge(pc))
                    continue;
                System.out.print("请选择一个下载软件：");
                pc.showSoftware2();
                String downloadSoftwareName;
                while (pc.searchSoftware(downloadSoftwareName = input.next()) == null || !(pc.searchSoftware(downloadSoftwareName) instanceof DownloadSoftware) || !pc.searchSoftware(downloadSoftwareName).isState()) {
                    System.out.print("没有此已安装的下载软件，请重新输入：");
                }
                pc.startSoftware(pc.searchSoftware(downloadSoftwareName));
                DownloadSoftware downloadSoftware = (DownloadSoftware) (pc.searchSoftware(downloadSoftwareName));
                System.out.println("1：普通软件/2：下载软件");
                int n;
                while ((n = input.nextInt()) != 1 && n != 2)
                    System.out.print("选择错误，请重新输入：");
                if (n == 1) {
                    System.out.print("下载软件的名字：");
                    String softwareName = input.next();
                    System.out.print("大小：");
                    int size = input.nextInt();
                    System.out.print("请输入安装的盘符：");
                    String diskName;
                    while (pc.searchDisk(diskName = input.next()) == null)
                        System.out.print("没有这个盘，请重新输入：");
                    Software software = new Software(softwareName, size);//普通软件
                    if (pc.downloadSoft(downloadSoftware, software, pc.searchDisk(diskName)))
                        System.out.println("下载软件 " + softwareName + " 成功!");
                    else
                        System.out.println("下载软件 " + softwareName + " 失败");
                } else {
                    System.out.print("下载软件的名字：");
                    String downloadSoftwareName1 = input.next();
                    System.out.print("大小：");
                    int size = input.nextInt();
                    System.out.print("请输入安装的盘符：");
                    String diskName;
                    while (pc.searchDisk(diskName = input.next()) == null)
                        System.out.print("没有这个盘，请重新输入：");
                    Software software = new DownloadSoftware(downloadSoftwareName1, size);//下载软件
                    if (pc.downloadSoft(downloadSoftware, software, pc.searchDisk(diskName)))
                        System.out.println("下载软件 " + downloadSoftwareName1 + " 成功!");
                    else
                        System.out.println("下载软件 " + downloadSoftwareName1 + " 失败");
                }
            } else if (choice == 6) {
                System.out.println("--查看各盘情况--");
                if (!judge(pc))
                    continue;
                System.out.print("请输入盘符：");
                String diskName;
                while (pc.searchDisk(diskName = input.next()) == null)
                    System.out.print("没有这个盘，请重新输入：");
                pc.searchDisk(diskName).show();
            } else if (choice == 9) {
                System.out.println("--退出--");
                break;
            } else {
                System.out.println("输入错误，请重新输入");
                continue;
            }
        }


    }

    static OS[] os = {
            new OS("win7", 20000),  //单位为兆
            new OS("win10", 20000),
            new OS("win11", 20000),
    };


    //辅助方法，判断Pc各种状态
    public static boolean judge(Pc pc) {
        if (!pc.isState()) {
            System.out.println("请先启动");
            return false;
        }
        if (!pc.judgeHasFormat()) {
            System.out.println("请先格式化");
            return false;
        }
        if (!pc.judgeHasOS()) {
            System.out.println("请先安装操作系统");
            return false;
        }
        return true;
    }


    //辅助方法，寻找名字对应的操作系统
    public static OS searchOS(String name) {
        for (int i = 0; i < os.length; i++) {
            if (os[i].getName().equals(name))
                return os[i];
        }
        return null;
    }

}
